package leetcode;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * User: Ghost
 * Date: 14-6-11
 * Time: 上午10:24
 * To change this template use File | Settings | File Templates.
 */
public class Roman_to_Integer {
    public static final HashMap<String,Integer> map = new HashMap<String, Integer>();
    static {
        map.put("I",1);
        map.put("V",5);
        map.put("X",10);
        map.put("C",100);
        map.put("L",50);
        map.put("D",500);
        map.put("M",1000);
    }
    public static int romanToInt(String s) {
        int pre,rst;
        pre = map.get(s.charAt(0)+"");
        rst = pre;
        for(int i=1;i<s.length();i++){
            if(pre>=map.get(s.charAt(i)+"")){
                rst+=map.get(s.charAt(i)+"");
            }
            else{
                rst = rst - 2*pre + map.get(s.charAt(i)+"");
            }
            pre = map.get(s.charAt(i)+"");
        }
        return rst;
    }
    public static void main(String[]  args){
        System.out.println(romanToInt("MMMCMXCIX"));
    }
}
